package com.pet.pet.app.requst;

import com.pet.pet.app.parent.AncestorsProcessor;

import com.pet.pet.utlis.UPetUnifiedV;
import com.pet.pet.utlis.URLParam;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class Request extends AncestorsProcessor {


    /**
     *  get 请求
     * @param url 请求路径，
     * @param uId 用户id，相当于qq，
     * @param cookie 需要携带的 cookie
     * @param param url携带的参数
     * @return
     */
    public UPetUnifiedV sendGet(String url, Long uId, String cookie, URLParam...param){
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        if (val.isNullObj(uId)){
            return UPetUnifiedV.error("uId is Null !");
        }
        if (val.isNullString(cookie)){
            return UPetUnifiedV.error("cookie is Null !");
        }
        Boolean flag = param != null && param.length > 0;
        String paramIsNotNull = flag ? "?" : "";
        if (flag){
            for (int i = 0; i < param.length; i++) {
                if (i == 0){
                    paramIsNotNull += param[i].getK() + "=" + param[i].getV();
                }else {
                    paramIsNotNull += "&" + param[i].getK() + "=" + param[i].getV();
                }
            }
        }
        HttpGet httpGet = new HttpGet(url+paramIsNotNull);
        httpGet.setHeader("Content-Type", "application/json;charset=utf8");
        httpGet.setHeader("cookie",cookie);
        // 响应模型
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            if (!val.isEqual(response.getStatusLine().getStatusCode(),200)){
                saveLog(response.getStatusLine().getStatusCode(),"statusCode != 200 Keep a record! "+response.getStatusLine()+"",uId,NO);
                return UPetUnifiedV.error(response.getStatusLine().getStatusCode(),response.getStatusLine()+"");
            }
            HttpEntity responseEntity = response.getEntity();
            if (!val.isNullObj(responseEntity)) {
                return UPetUnifiedV.ok().put(DATA_KEY,EntityUtils.toString(responseEntity));
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            saveLog(CATCH_CODE,"catch ClientProtocolException Keep a record！"+e.getMessage(),uId,YES);
            modify(uId, USER_STATE_NO);
            return UPetUnifiedV.error(CATCH_CODE,e.getMessage());
        } catch (ParseException e) {
            e.printStackTrace();
            saveLog(CATCH_CODE,"catch ParseException Keep a record！"+e.getMessage(),uId,YES);
            modify(uId, USER_STATE_NO);
            return UPetUnifiedV.error(CATCH_CODE,e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            saveLog(CATCH_CODE,"catch IOException Keep a record！"+e.getMessage(),uId,YES);
            modify(uId, USER_STATE_NO);
            return UPetUnifiedV.error(CATCH_CODE,e.getMessage());
        } finally {
            try {
                // 释放资源
                if (!val.isNullObj(httpClient)) {
                    httpClient.close();
                }
                if (!val.isNullObj(response)) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                saveLog(CATCH_CODE,"finally IOException Keep a record！"+e.getMessage(),uId,YES);
                modify(uId, USER_STATE_NO);
            }
        }
        saveLog(-4," Stop at the end The request did nothing",uId,NO);
        return UPetUnifiedV.error();
    }

}